package review.organization.action;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import review.config.Constants;
import review.entity.Specialist;
import review.entity.model.SpecialistModel;
import review.organization.service.SpecialistService;

@Controller
@RequestMapping("/organization")
@Api(tags = { "专家操作接口" })
public class SpecialistAction {

  @Autowired
  private SpecialistService specialistService;

  @ApiOperation(value = "添加专家", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/add_specialist", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object add(SpecialistModel model) {
    JSONObject obj = new JSONObject();
    try {
      specialistService.addSpecialist(model);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "分页查询专家", notes = "", httpMethod = "GET")
  @RequestMapping(value = "/query_specialist", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object query(Specialist specialist, @RequestParam(defaultValue = "1") int page,
      @RequestParam(defaultValue = "10") int size) {
    JSONObject obj = new JSONObject();
    try {
      Page<SpecialistModel> specialistModelPage = specialistService.findAll(page, size, specialist);
      obj.put(Constants.SUCCESS, true);
      obj.put(Constants.DATA, specialistModelPage);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "删除专家", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/delete_specialist", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object delete(Specialist specialist) {
    JSONObject obj = new JSONObject();
    try {
      specialistService.deleteSpecialist(specialist);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

  @ApiOperation(value = "修改单位", notes = "", httpMethod = "POST")
  @RequestMapping(value = "/update_specialist", produces = "application/json;charset=UTF-8")
  @ResponseBody
  public Object update(SpecialistModel model) {
    JSONObject obj = new JSONObject();
    try {
      specialistService.updateSpecialist(model);
      obj.put(Constants.SUCCESS, true);
    } catch (Exception e) {
      obj.put(Constants.MESSAGE, e.getMessage());
      obj.put(Constants.SUCCESS, false);
    }
    return obj;
  }

}
